//
//  UIDevice+BZHelper.m
//  MUPFoundation
//
//  Created by devp on 14-12-12.
//  Copyright (c) 2014年 ND. All rights reserved.
//

#import "UIDevice+BZHelper.h"
//#import <MUPFoundationProfiler/MUPFoundationProfiler.h>
// for video
#import <AudioToolbox/AudioToolbox.h>
// for auido
#import <AVFoundation/AVFoundation.h>

@implementation UIDevice (BZHelper)
+ (BOOL)mup_canRecord {
    __block BOOL bCanRecord = YES;
    // iOS7可以禁用麦克风，需要麦克风权限判断，判断麦克风是否开启,iOS7新增requestRecordPermission
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
        [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
            bCanRecord = granted;
        }];
    }
    return bCanRecord;
}

+ (BOOL)mup_isHeadsetPluggedIn {
    AVAudioSessionRouteDescription *route = [[AVAudioSession sharedInstance] currentRoute];
    for (AVAudioSessionPortDescription *desc in [route outputs]) {
        if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
            return YES;
    }
    return NO;
}

#pragma mark --拍照扩展
+ (BOOL)mup_canAccessCamera {
    
    // iOS7可以禁用相机，需要相机权限判断，判断相机是否开启;
    //== 2,是由于iOS7无相机权限的枚举值AVAuthorizationStatusDenied值 为2，由于AVAuthorizationStatus是IOS7专有枚举，为了能用ios5SDK编译只能写死
    if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)]) {
        if((NSInteger)[AVCaptureDevice performSelector:(@selector(authorizationStatusForMediaType:)) withObject:AVMediaTypeVideo] == 2){
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)mup_dialPhone:(NSString *)phoneNumber {
    if (!phoneNumber) {
        return NO;
    }
    
    NSString *url = nil;
    if ([phoneNumber hasPrefix:@"tel://"]) {
        url = [phoneNumber copy];
    } else {
        url = [NSString stringWithFormat:@"tel://%@", [phoneNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    
    if ([url length] <= 6) {
        return NO;
    }
    
    BOOL dialResult = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    if (!dialResult) {
        NSLog(@"dial phone number failed, %@, %@", [self model], url);
    }
    
    return dialResult;
}

- (BOOL)mup_sendSMS:(NSString *)phoneNumber {
    if (!phoneNumber) {
        return NO;
    }
    
    NSString *url = nil;
    if ([phoneNumber hasPrefix:@"sms://"]) {
        url = [phoneNumber copy];
    } else {
        url = [NSString stringWithFormat:@"sms://%@", [phoneNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    
    if ([url length] <= 6) {
        return NO;
    }
    
    BOOL dialResult = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    if (!dialResult) {
        NSLog(@"send sms failed, %@, %@", [self model], url);
    }
    
    return dialResult;
}

- (BOOL)mup_sendEMail:(NSString *)emailAddress {
    if (!emailAddress) {
        return NO;
    }
    
    NSString *url = nil;
    if ([emailAddress hasPrefix:@"mailto://"]) {
        url = [emailAddress copy];
    } else {
        url = [NSString stringWithFormat:@"mailto://%@", [emailAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    
    if ([url length] <= 6) {
        return NO;
    }
    
    BOOL dialResult = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    if (!dialResult) {
        NSLog(@"send email failed, %@, %@", [self model], url);
    }
    
    return dialResult;
}
@end
